#include void stampa() { char nomefile[100]; FILE * file; printf("Nome file?\n"); scanf("%s",nomefile); printf("Tento di aprire: [%s]\n", nomefile); file=fopen(nomefile, "r+"); if(file) { while(!feof(file)) { putchar(fgetc(file)); } } else { printf("Impossibile aprire %s in lettura\n",nomefile); } } void copy() { char nomefile1[100]; char nomefile2[100]; FILE * file1; FILE * file2; printf("Nome file1?\n"); scanf("%s",nomefile1); printf("Nome file2?\n"); scanf("%s",nomefile2); printf("Tento di aprire in lettura: [%s]\n", nomefile1); file1=fopen(nomefile1, "r+"); if(!file1) { printf("Impossibile aprire [%s] in lettura!\n", nomefile1); return; } printf("Tento di aprire in scrittura: [%s]\n", nomefile2); file2=fopen(nomefile2, "w"); if(!file2) { printf("Impossibile aprire [%s] in scrittura!\n", nomefile2); return; } while(!feof(file1)) { fputc(fgetc(file1), file2); } fclose(file2); fclose(file1); } void append() { char nomefile1[100]; char nomefile2[100]; FILE * file1; FILE * file2; printf("Nome file1?\n"); scanf("%s",nomefile1); printf("Nome file2?\n"); scanf("%s",nomefile2); printf("Tento di aprire in lettura: [%s]\n", nomefile1); file1=fopen(nomefile1, "r+"); if(!file1) { printf("Impossibile aprire [%s] in lettura!\n", nomefile1); return; } printf("Tento di aprire in append: [%s]\n", nomefile2); file2=fopen(nomefile2, "a"); if(!file2) { printf("Impossibile aprire [%s] in append!\n", nomefile2); return; } while(!feof(file1)) { fputc(fgetc(file1), file2); } fclose(file2); fclose(file1); } void grep() { char nomefile[100]; char stringa[100]; FILE * file; printf("Nome file?\n"); scanf("%s",nomefile); printf("Stringa?\n"); scanf("%s",stringa); printf("Tento di aprire: [%s]\n", nomefile); file=fopen(nomefile, "r+"); if(file) { char linea[1000]; while(!feof(file)) { fgets(linea, 999, file); if(strstr(linea, stringa)) { printf("Stringa trovata!\n"); return; } } printf("Stringa non trovata!\n"); } else { printf("Impossibile aprire %s in lettura\n",nomefile); } } int menu() { int scelta; while(1) { printf("1: STAMPA\n"); printf("2: COPY\n"); printf("3: APPEND\n"); printf("4: GREP\n"); scanf("%d", &scelta); if(scelta>=1 && scelta <= 4)break; } return scelta; } void main() { int a; while(1) { a=menu(); switch(a) { case 1: stampa(); break; case 2: copy(); break; case 3: append(); break; case 4: grep(); break; } } }ÿ